<script setup>
import { ref, onMounted } from 'vue'
import apiClient from '../utils/api'

const serverInfo = ref(null)
const clientInfo = ref(null)
const isLoading = ref(true)
const errorMessage = ref('')

onMounted(async () => {
  try {
    const response = await apiClient.get('/')
    
    if (response.status === 200) {
      serverInfo.value = response.data.server
      clientInfo.value = response.data.client
    }
  } catch (error) {
    handleError(error)
  } finally {
    isLoading.value = false
  }
})

const handleError = (error) => {
  if (error.response) {
    switch (error.response.status) {
      case 404:
        errorMessage.value = 'API endpoint not found'
        break
      case 500:
        errorMessage.value = 'Server internal error'
        break
      default:
        errorMessage.value = `Request failed: ${error.message}`
    }
  } else {
    errorMessage.value = 'Network error - Please check your connection'
  }
}
</script>

<template>
  <div class="home-container">
    <h1>System Information</h1>
    
    <div v-if="isLoading" class="loading">
      <div class="spinner"></div>
      <p>Loading system information...</p>
    </div>

    <div v-else-if="errorMessage" class="error">
      ❌ Error: {{ errorMessage }}
    </div>

    <div v-else class="info-grid">
      <section class="server-info">
        <h2>Server Status</h2>
        <div class="info-item">
          <label>Version:</label>
          <span>{{ serverInfo?.version || 'N/A' }}</span>
        </div>
        <div class="info-item">
          <label>Environment:</label>
          <span>{{ serverInfo?.environment || 'N/A' }}</span>
        </div>
        <div class="info-item">
          <label>Uptime:</label>
          <span>{{ serverInfo?.uptime ? `${serverInfo.uptime} seconds` : 'N/A' }}</span>
        </div>
      </section>

      <section class="client-info">
        <h2>Client Details</h2>
        <div class="info-item">
          <label>IP Address:</label>
          <span>{{ clientInfo?.ip || 'N/A' }}</span>
        </div>
        <div class="info-item">
          <label>User Agent:</label>
          <span class="user-agent">{{ clientInfo?.user_agent || 'N/A' }}</span>
        </div>
        <div class="info-item">
          <label>Request Time:</label>
          <span>{{ new Date(clientInfo?.timestamp * 1000).toLocaleString() }}</span>
        </div>
      </section>
    </div>
  </div>
</template>

<style scoped>
.home-container {
  max-width: 1200px;
  margin: 2rem auto;
  padding: 0 1rem;
}

h1 {
  text-align: center;
  color: #2c3e50;
  margin-bottom: 2rem;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 2rem;
  margin-top: 2rem;
}

section {
  background: #f8f9fa;
  border-radius: 8px;
  padding: 1.5rem;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

h2 {
  color: #34495e;
  margin-bottom: 1rem;
  font-size: 1.25rem;
}

.info-item {
  display: flex;
  justify-content: space-between;
  margin: 0.75rem 0;
  padding: 0.5rem;
  background: white;
  border-radius: 4px;
}

.info-item label {
  font-weight: 600;
  color: #7f8c8d;
}

.user-agent {
  word-break: break-all;
  max-width: 200px;
}

.loading {
  text-align: center;
  padding: 2rem;
}

.spinner {
  width: 40px;
  height: 40px;
  margin: 0 auto;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #3498db;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.error {
  color: #e74c3c;
  background: #f8d7da;
  padding: 1rem;
  border-radius: 4px;
  text-align: center;
  margin: 2rem 0;
}
</style>